<template>
    <div id="novel_chapters">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/novel_list' }">小说列表</el-breadcrumb-item>
            <el-breadcrumb-item>小说章节</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-form ref="form" label-width="100" size="small">
                <el-form-item label="章节标题:">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="内容:">
                    <span>{{wordNumber}}个字数</span>
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 28}"
                            placeholder="请输入内容"
                            class="textarea_input"
                            @input="watchWordNumber"
                            v-model="form.info">
                    </el-input>
                </el-form-item>
              <el-form-item label="作者有话要说:">
                <el-input
                    type="textarea"
                    placeholder="请输入内容"
                    class="textarea_input"
                    v-model="form.writer_say">
                </el-input>
              </el-form-item>
              <el-form-item label="审核失败原因:" v-if="form.fail_reason">
                {{form.fail_reason}}
              </el-form-item>

              <el-form-item label="是否定时提交">
                <el-select v-model="submitType" placeholder="请选择">
                  <el-option label="立刻提交审核" value=0></el-option>
                  <el-option label="定时提交审核" value=1></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="提交时间" v-if="submitType == 1">

                <el-form-item prop="submit_time">
                  <el-date-picker
                      type="datetime"
                      placeholder="选择日期时间"
                      v-model="submit_time"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      style="width: 100%;"
                      default-value-type="datetime"
                      :picker-options="pickerOptions"
                  ></el-date-picker>
                </el-form-item>

              </el-form-item>


                <el-form-item label="">
                    <el-button size="small" @click="back">返回</el-button>
                    <el-button type="primary" @click="dialogAffirmEdit" size="small">保存草稿</el-button>
                    <el-button type="success" @click="submitAudit" size="small">提交审核</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
  export default {
    name: "novel_chapters",
    data() {
      return {
        form: {
          anid: '',
          chaps: 0,
          version: '',
          title: '',
          info: '',
          writer_say: '',
          fail_reason: '',
          submit_at: '',
        },
        id: '',
        anid: '',
        wordNumber: '0',
        submitType: '0',
        submit_time: '',

        pickerOptions: {
          disabledDate(time) {
            // 禁止选择当前时间之前的日期
            return time.getTime() < Date.now() - 8.64e7; // 8.64e7 是一天的毫秒数
          },

        }

      }
    },
    created() {
      this.id = this.$route.query.id;
      this.anid = this.$route.query.anid;
      if (this.id) {
        this.$axios.get('writer/chapter/detail',{'anid':this.anid, 'id': this.id }).then((res) => {
          this.form = res.data;
          this.watchWordNumber();
        });
      }
    },
    methods: {

      watchWordNumber() {
        let str = this.form.info.replace(/[\s\r\n]+/g, '');
        this.wordNumber = str.length
      },

      dialogAffirmEdit() {
          //编辑
          this.form = Object.assign({anid: this.anid}, this.form);
          this.form.anid = this.anid;
          this.$axios.post('writer/chapter/save-draft', this.form).then((res) => {
            this.$message.success('成功');
            this.back();
          });

      },
      submitAudit() {
        //编辑

        if (this.submitType == 0) {
          this.form.submit_at = '';
        } else {
          this.form.submit_at = new Date(this.submit_time).getTime()/1000;
        }


        this.form = Object.assign({anid: this.anid}, this.form);
        this.form.anid = this.anid;
        this.$axios.post('writer/chapter/push', this.form).then((res) => {
          this.$message.success('成功');
          this.back();
        });
      },
      back() {
        this.$router.go(-1)
      }
    }
  }
</script>

<style scoped lang="less">
  .textarea_input textarea {
    text-indent: 40px; /* 假设两个汉字的宽度为40px */
  }

</style>